//
//  LJNLocation.m
//  UserUse
//
//  Created by 李佳楠 on 2016/10/14.
//  Copyright © 2016年 CJPC. All rights reserved.
//

#import "LJNLocation.h"
#define LJNCurrentCity         @"LJNCurrentCity"
#define LJNCurrentCountry      @"LJNCurrentCountry"
#define LJNCurrentDetailPlace  @"LJNCurrentDetailPlace"
#define LJNCurrentsubLocality  @"LJNCurrentsubLocality"
static LJNLocation *instance;
@interface LJNLocation () <CLLocationManagerDelegate>
{
    NSString *country;//国家
    NSString *city;//当前所在城市
    NSString *currentPlace;//当前具体位置
    NSString *subLocality;//当前所在区县
    
}

@property (nonatomic, strong) CLLocationManager *locationManager;
@end
@implementation LJNLocation
+(instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        @synchronized (self) {
            instance = [[self alloc]init];
        }
    });
    return instance;
}
-(instancetype)init
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        @synchronized (self) {
            instance = [super init];
        }
    });
    return instance;
}
-(void)beginLocation
{
    
    _locationManager = [[CLLocationManager alloc]init];
    [_locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    _locationManager.delegate = self;
    [_locationManager requestWhenInUseAuthorization];
    [_locationManager startUpdatingLocation];
    NSLog(@"%s",__FUNCTION__);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    NSLog(@"%s",__FUNCTION__);
    CLLocation *newLocation = locations[0];
    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
    NSLog(@"旧的经度：%f,旧的纬度：%f",oldCoordinate.longitude,oldCoordinate.latitude);
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder reverseGeocodeLocation:newLocation
                       completionHandler:^(NSArray *placemarks, NSError *error){
                           
                           for (CLPlacemark *place in placemarks) {
                               //                           NSLog(@"name,%@",place.name);                       // 位置名
                               //                           NSLog(@"thoroughfare,%@",place.thoroughfare);       // 街道
                               //                           NSLog(@"subThoroughfare,%@",place.subThoroughfare); // 子街道
                               //                           NSLog(@"locality,%@",place.locality);               // 市
                               //                           NSLog(@"subLocality,%@",place.subLocality);         // 区
                               //                           NSLog(@"country,%@",place.country);                 // 国家
                               currentPlace = place.thoroughfare;
                               country = place.country;
                               city = place.locality;
                               subLocality = place.subLocality;
                               [city saveToNSDefaultsWithKey:LJNCurrentCity];
                               [currentPlace saveToNSDefaultsWithKey:LJNCurrentDetailPlace];
                               [country saveToNSDefaultsWithKey:LJNCurrentCountry];
                               [subLocality saveToNSDefaultsWithKey:LJNCurrentsubLocality];
                               
                           }
                           
                       }];
   
}
-(void)endLocation
{
    [_locationManager stopUpdatingLocation];
    _locationManager = nil;
}
-(NSString *)city
{
    if (!city) {
        city = [kUserDefaults objectForKey:LJNCurrentCity];
    }
    return city;
}
-(NSString *)country
{
    if (!country) {
        country = [kUserDefaults objectForKey:LJNCurrentCountry];
    }
    return country;
}
-(NSString *)currentPlace
{
    if (!currentPlace) {
        currentPlace = [kUserDefaults objectForKey:LJNCurrentDetailPlace];
    }
    return currentPlace;
}
-(NSString *)subLocality
{
    if (!subLocality) {
        subLocality = [kUserDefaults objectForKey:LJNCurrentsubLocality];
    }
    return subLocality;
}
@end
